package gblend.db;


import java.sql.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import javax.swing.JTree;
import javax.swing.tree.*;
/*
* Copyright 2010, Center for Advanced Information Systems,Nanyang Technological University
*
* File name: LoadSchema.java
*
* Abstract: Load the unique labels in the database
*
* Current Version:      0.1
* Auther:               Jin Changjiu
* Modified Date:        Mar.3, 2010
*
*/

/**
 *
 * @author cjjin
 */
public class LoadSchema {

    private Connection conn;
    private Statement stmt = null ;
    private ResultSet rs = null;
    private static DefaultMutableTreeNode top;

    /**
     * Make a JTree with db name as head and node labels as child & add to the panel on the left under the
     * combo box.
     * @param dbinfo
     * @param tree
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public boolean loadDB(DatabaseInfo dbinfo,JTree tree) throws SQLException, ClassNotFoundException
    {

        String dbname =dbinfo.getDbName();
        top = new DefaultMutableTreeNode(dbname);
        String reallabels[]=dbinfo.getLabels();

        for(int i = 0; i< reallabels.length; i++)
        {
            String label = reallabels[i];
            DefaultMutableTreeNode unilabel = new DefaultMutableTreeNode(label);
            top.add(unilabel);
        }
        //cell render
        tree.setModel(new DefaultTreeModel(top));
        //Mechanism for transfer of data to and from the component
        tree.setTransferHandler(new TreeTransferHandler());
        tree.setCellRenderer(new NodeCellRenderer());
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

        return true;
   }


    public static boolean isDatasource(DefaultMutableTreeNode node)
    {
      if(node == top)
      {
        return true;
      }
      else
       return false;
    }
    
}
